package com.rabbiter.pm.controller;

import okhttp3.*;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

public class LicensePlateRecognition {
    private static final String LICENSE_PLATE_URL = "https://aip.baidubce.com/rest/2.0/ocr/v1/license_plate";

    public static void recognizeLicensePlate(String accessToken, File imageFile) throws IOException {
        OkHttpClient client = new OkHttpClient();

        // 构建请求体
        RequestBody requestBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("access_token", accessToken)
                .addFormDataPart("image", imageFile.getName(),
                        RequestBody.create(imageFile, MediaType.parse("image/png")))
                .build();

        // 构建请求
        Request request = new Request.Builder()
                .url(LICENSE_PLATE_URL)
                .post(requestBody)
                .build();

        // 发送请求并处理响应
        Response response = client.newCall(request).execute();
        if (response.isSuccessful()) {
            System.out.println("车牌识别结果：" + response.body().string());
        } else {
            System.out.println("车牌识别失败：" + response.body().string());
        }
    }

    public static void main(String[] args) throws IOException {
        // 获取AccessToken
        String accessToken = BaiduOCR.getAccessToken();



        // 读取图片文件
        File imageFile = new File("C:\\Users\\Administrator\\Desktop\\ceshi.png");

        // 调用车牌识别
        recognizeLicensePlate(accessToken, imageFile);
    }
}